//
//  BA_ADLaunchManager.m
//  BaseProject
//
//  Created by 王洋 on 2020/9/21.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "XHLaunchAd.h"

#import "BA_ADLaunchManager.h"
#import "HomeADItemInfoModel.h"

@interface BA_ADLaunchManager ()

@property (nonatomic, strong) HomeADItemInfoModel *model;

@property (nonatomic, strong) HomeADItemEventModel *eventModel;

@end

@implementation BA_ADLaunchManager

+(void)load{
    [self shareManager];
}

+(BA_ADLaunchManager *)shareManager{
    static BA_ADLaunchManager *instance = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken,^{
        instance = [[BA_ADLaunchManager alloc] init];
    });
    return instance;
}


- (instancetype)init{
    self = [super init];
    if (self) {
        //在UIApplicationDidFinishLaunching时初始化开屏广告,做到对业务层无干扰,当然你也可以直接在AppDelegate didFinishLaunchingWithOptions方法中初始化
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
            if (!kCloseAd) {
                //初始化开屏广告
                [self setupXHLaunchAd];
            }
        }];
    }
    return self;
}

-(void)setupXHLaunchAd{
    
//    [self example01];
    
    NSDictionary *startPage = [[TTUserModel sharedObject].userModelDefault valueForKey:@"BA_startPage"];
    
    if ([startPage isKindOfClass:[NSDictionary class]]) {
        self.model = [HomeADItemInfoModel modelWithDictionary:startPage];
        //self.model.advBackImg = [TT_OSSManager reformWebPUrl:self.model.advBackImg];
        //self.model.advBackImg = self.model.advBackImg;
        //self.model.advBackImg = @"https://wx2.sinaimg.cn/mw690/005wuWGagy1hs9ylp8rwkj30u011iwj1.jpg";

        NSDictionary *sysAdvEvent = [startPage valueForKey:@"sysAdvEvent"];
        self.eventModel = [HomeADItemEventModel modelWithDictionary:sysAdvEvent];
        
        //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
        [XHLaunchAd setLaunchSourceType:SourceTypeLaunchScreen];
        
        //配置广告数据
        XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration new];
        //广告停留时间
        imageAdconfiguration.duration = 5;
        //广告frame
        imageAdconfiguration.frame = CGRectMake(0, 0, TTScreenWidth, TTScreenHeight);
        //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
        //注意本地广告图片,直接放在工程目录,不要放在Assets里面,否则不识别,此处涉及到内存优化
        imageAdconfiguration.imageNameOrURLString = self.model.advBackImg;
        //设置GIF动图是否只循环播放一次(仅对动图设置有效)
        imageAdconfiguration.GIFImageCycleOnce = NO;
        //图片填充模式
        imageAdconfiguration.contentMode = UIViewContentModeScaleAspectFill;
        //广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
        imageAdconfiguration.openModel = self.eventModel;
        //广告显示完成动画
        imageAdconfiguration.showFinishAnimate =ShowFinishAnimateFadein;
        //广告显示完成动画时间
        imageAdconfiguration.showFinishAnimateTime = 0.8;
        //跳过按钮类型
        imageAdconfiguration.skipButtonType = SkipTypeRoundProgressText;
        //后台返回时,是否显示广告
        imageAdconfiguration.showEnterForeground = NO;
        //设置要添加的子视图(可选)
        //imageAdconfiguration.subViews = [self launchAdSubViews];
        //显示开屏广告
        [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
    }
}

#pragma mark - subViews
-(NSArray<UIView *> *)launchAdSubViews_alreadyView{
    
    CGFloat y = XH_FULLSCREEN ? 46:22;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-140, y, 60, 30)];
    label.text  = @"已预载";
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.layer.cornerRadius = 5.0;
    label.layer.masksToBounds = YES;
    label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    return [NSArray arrayWithObject:label];
    
}

-(NSArray<UIView *> *)launchAdSubViews{
    
    CGFloat y = XH_FULLSCREEN ? 54 : 30;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-170, y, 60, 30)];
    label.text  = @"subViews";
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.layer.cornerRadius = 5.0;
    label.layer.masksToBounds = YES;
    label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    return [NSArray arrayWithObject:label];
    
}

#pragma mark - customSkipView
//自定义跳过按钮
-(UIView *)customSkipView{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor =[UIColor orangeColor];
    button.layer.cornerRadius = 5.0;
    button.layer.borderWidth = 1.5;
    button.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    CGFloat y = XH_FULLSCREEN ? 54 : 30;
    button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-100,y, 85, 30);
    [button addTarget:self action:@selector(skipAction) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

//跳过按钮点击事件
-(void)skipAction{
    
    //移除广告
    [XHLaunchAd removeAndAnimated:YES];
}

#pragma mark - XHLaunchAd delegate - 倒计时回调
/**
 *  倒计时回调
 *
 *  @param launchAd XHLaunchAd
 *  @param duration 倒计时时间
 */
-(void)xhLaunchAd:(XHLaunchAd *)launchAd customSkipView:(UIView *)customSkipView duration:(NSInteger)duration{
    //设置自定义跳过按钮时间
    UIButton *button = (UIButton *)customSkipView;//此处转换为你之前的类型
    //设置时间
    [button setTitle:[NSString stringWithFormat:@"自定义%lds",duration] forState:UIControlStateNormal];
}

#pragma mark - XHLaunchAd delegate - 其他
/**
 广告点击事件回调(return YES移除广告,NO不移除广告)
 */
-(BOOL)xhLaunchAd:(XHLaunchAd *)launchAd clickAtOpenModel:(id)openModel clickPoint:(CGPoint)clickPoint{
    
    NSLog(@"广告点击事件");
    
    //openModel即配置广告数据设置的点击广告时打开页面参数(configuration.openModel)
    
    if(openModel == nil) return NO;
    
    if ([openModel isKindOfClass:[HomeADItemEventModel class]]) {
        
        HomeADItemEventModel *model = (HomeADItemEventModel *)openModel;
        [[TT_JumpManager sharedManager] jumpWithModel:model completion:^(BOOL success, NSString * _Nonnull message) {
            
        }];
    }
    
    return YES;//YES移除广告,NO不移除广告
}

/**
 *  图片本地读取/或下载完成回调
 *
 *  @param launchAd  XHLaunchAd
 *  @param image 读取/下载的image
 *  @param imageData 读取/下载的imageData
 */
-(void)xhLaunchAd:(XHLaunchAd *)launchAd imageDownLoadFinish:(UIImage *)image imageData:(NSData *)imageData{
    
//    NSLog(@"图片下载完成/或本地图片读取完成回调");
}

/**
 *  视频本地读取/或下载完成回调
 *
 *  @param launchAd XHLaunchAd
 *  @param pathURL  视频保存在本地的path
 */
-(void)xhLaunchAd:(XHLaunchAd *)launchAd videoDownLoadFinish:(NSURL *)pathURL{
    
    NSLog(@"video下载/加载完成 path = %@",pathURL.absoluteString);
}

/**
 *  视频下载进度回调
 */
-(void)xhLaunchAd:(XHLaunchAd *)launchAd videoDownLoadProgress:(float)progress total:(unsigned long long)total current:(unsigned long long)current{
    
    NSLog(@"总大小=%lld,已下载大小=%lld,下载进度=%f",total,current,progress);
}

/**
 *  广告显示完成
 */
-(void)xhLaunchAdShowFinish:(XHLaunchAd *)launchAd{
    
//    NSLog(@"广告显示完成");
}

@end
